package com.jojo.bubble.controller;

import com.jojo.bubble.entity.Agent;
import com.jojo.bubble.entity.Dev;
import com.jojo.bubble.entity.Shop;
import com.jojo.bubble.service.AgentService;
import com.jojo.bubble.service.DevService;
import com.jojo.bubble.common.domain.AjaxResult;
import com.jojo.bubble.service.ShopService;
import com.jojo.bubble.utils.QRCodeGenerator;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.ObjectUtils;
import com.jojo.bubble.common.enums.JsonResultEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备表(Dev)表控制层
 *
 * @author jojo
 * @since 2023-07-21 04:47:28
 */
@RestController
@RequestMapping("/admin/dev")
public class DevController {
    /**
     * 服务对象
     */
    @Resource
    private DevService devService;
    @Resource
    private AgentService agentService;

    @Resource
    private ShopService shopService;


    @GetMapping("/list")
    public AjaxResult getList(Dev record) {
        List<Dev> devList = devService.getList(record);
        for (Dev dev : devList) {
            Agent agent = agentService.getInfo(dev.getAgentId());
            if (!ObjectUtils.isEmpty(agent)) {
                dev.setAgentName(agent.getName());
            } else {
                dev.setAgentName("未关联");
            }
            agent = agentService.getInfo(dev.getOwnAgentId());
            if (!ObjectUtils.isEmpty(agent)) {
                dev.setOwnAgentName(agent.getName());
            } else {
                dev.setAgentName("未关联");
            }
            Shop shop = shopService.getInfo(dev.getBoxNo());
            if (!ObjectUtils.isEmpty(shop)) {
                dev.setShopName(shop.getName());
            } else {
                dev.setShopName("未关联");
            }
        }
        return AjaxResult.success(devList);
    }

    @GetMapping("/code")
    public AjaxResult getCode(Integer id) {
        Dev dev = devService.getInfo(id);
        String url = QRCodeGenerator.getCode(dev.getCode(), dev.getId());
        if (StringUtils.hasLength(url)) {
            dev.setRemark(url);
            devService.update(dev);
        }
        return AjaxResult.success(dev);
    }

    @GetMapping("/info")
    public AjaxResult getInfo(Integer id) {
        Dev dev = devService.getInfo(id);
        return AjaxResult.success(dev);
    }

    @PostMapping("/insert")
    public AjaxResult insert(Dev record) {
        devService.insert(record);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    public AjaxResult update(Dev record) {
        Dev dev = devService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(dev)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        devService.update(record);
        return AjaxResult.success();
    }

    @PostMapping("/updateDevTypeByCode")
    public AjaxResult updateDevTypeByCode(Dev record){
        Dev dev = devService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(dev)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        devService.updateDevTypeByCode(record);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    public AjaxResult delete(int id) {
        devService.delete(id);
        return AjaxResult.success();
    }
}

